/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "PlayerModuleGenerator.hpp"

PlayerModuleGenerator::PlayerModuleGenerator():
  iRangingMinor(0),
  iPositionMinor(0),
  iSpeedCtrlMinor(0),
  iIMUMinor(0),
  iEnergyMinor(0),
  iWirelessMinor(0),
  iCoordinateDriveMinor(0),
  iImageMinor(0)
{
  dPrint(1,"Player Module Generator constructed");
}
PlayerModuleGenerator::~PlayerModuleGenerator(){

}

PlayerModule *PlayerModuleGenerator::GenerateNewPlayerModule(std::string aFilename,
                                                             MaCI::MaCICtrl::CMaCICtrlServer *aMaCICtrlServer){

  

  PlayerModule *pModule;
  //Read the first line of file and choose the right playerModule to construct
  FILE *file= fopen(aFilename.c_str(),"rt");
  char c[200] = "";
  fgets (c, 200,file );
  std::string firstLine(c);
  dPrint(10,"c '%s',first line '%s'",c,firstLine.c_str());
      
  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////
  ///////////CONSTRUCT HERE YOUR PLAYER MODULE//////////
  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////
  
  //////////////////////////////////////////////////////
  //////////////////RANGING FILE////////////////////////
  //////////////////////////////////////////////////////
  if(firstLine.compare("%MaCI_Ranging log file\n") == 0){
    //Create module and add it to player
    MaCI::Ranging::CRangingServer *rServer = new MaCI::Ranging::CRangingServer(aMaCICtrlServer,iRangingMinor);
    iRangingMinor++;
    RangingFilePlayer *rfplayer = new RangingFilePlayer(rServer,aFilename);
    pModule =static_cast<PlayerModule *>(rfplayer);
  
    dPrint(3,"Ranging file found!Created ranging File player");
    
    //////////////////////////////////////////////////////
    //////////////////POSITION FILE///////////////////////
    //////////////////////////////////////////////////////
  }else if(firstLine.compare(0,23,"%MaCI_Position log file") == 0){

    //Create module and add it to player
    MaCI::Position::CPositionServer *pServer = new MaCI::Position::CPositionServer(aMaCICtrlServer,iPositionMinor);
    iPositionMinor++;
    PositionFilePlayer *pfplayer = new PositionFilePlayer(pServer,aFilename);
    pModule =static_cast<PlayerModule *>(pfplayer);
    dPrint(3,"Position file found!Created position File player");

    //////////////////////////////////////////////////////
    //////////////////SPEEDCTRL FILE//////////////////////
    //////////////////////////////////////////////////////
  }else if(firstLine.compare("%MaCI_SpeedCtrl log file\n") == 0){
    //Create module and add it to player
    MaCI::SpeedCtrl::CSpeedCtrlServer *sServer = new MaCI::SpeedCtrl::CSpeedCtrlServer(aMaCICtrlServer,iSpeedCtrlMinor);
    iSpeedCtrlMinor++;
    SpeedCtrlFilePlayer *sfplayer = new SpeedCtrlFilePlayer(sServer,aFilename);
    pModule =static_cast<PlayerModule *>(sfplayer);

    dPrint(3,"SpeedCtrl file found!Created speedctrl file player");

    //////////////////////////////////////////////////////
    //////////////////IMU FILE////////////////////////////
    //////////////////////////////////////////////////////
  }else if(firstLine.compare("%MaCI_IMU log file\n") == 0){
    //Create module and add it to player
    MaCI::IMU::CIMUServer *iServer = new MaCI::IMU::CIMUServer(aMaCICtrlServer,iIMUMinor);
    iIMUMinor++;
    IMUFilePlayer *ifplayer = new IMUFilePlayer(iServer,aFilename);
    pModule =static_cast<PlayerModule *>(ifplayer);
    dPrint(3,"IMU file found!Created IMU file player");

    //////////////////////////////////////////////////////
    //////////////////ENERGY FILE/////////////////////////
    //////////////////////////////////////////////////////
  }else if(firstLine.compare("%MaCI_Energy log file\n") == 0){
    //Create module and add it to player
    MaCI::Energy::CEnergyServer *eServer = new MaCI::Energy::CEnergyServer(aMaCICtrlServer,iEnergyMinor);
    iEnergyMinor++;
    EnergyFilePlayer *efplayer = new EnergyFilePlayer(eServer,aFilename);
    pModule =static_cast<PlayerModule *>(efplayer);
    
    dPrint(3,"Energy file found!Created energy file player");
    
    //////////////////////////////////////////////////////
    //////////////////WIRELESS FILE///////////////////////
    //////////////////////////////////////////////////////
  }else if(firstLine.compare("%MaCI_Wireless log file\n") == 0){
    //Create module and add it to player
    MaCI::Wireless::CWirelessServer *wServer = new MaCI::Wireless::CWirelessServer(aMaCICtrlServer,iWirelessMinor);
    iWirelessMinor++;
    WirelessFilePlayer *wfplayer = new WirelessFilePlayer(wServer,aFilename);
    pModule =static_cast<PlayerModule *>(wfplayer);

    dPrint(3,"Wireless file found!Created wireless file player");
    //////////////////////////////////////////////////////
    //////////////////COORDINATEDRIVE FILE////////////////
    //////////////////////////////////////////////////////
  }else if(firstLine.compare("%MaCI_CoordinateDrive log file\n") == 0){
    //Create module and add it to player
    MaCI::CoordinateDrive::CCoordinateDriveServer *cServer = new MaCI::CoordinateDrive::CCoordinateDriveServer(aMaCICtrlServer,iCoordinateDriveMinor);
    iCoordinateDriveMinor++;
    CoordinateDriveFilePlayer *cfplayer = new CoordinateDriveFilePlayer(cServer,aFilename);
    pModule =static_cast<PlayerModule *>(cfplayer);

    dPrint(3,"CoordinateDrive file found!Created CoordinateDrive file player");
    
    //////////////////////////////////////////////////////
    //////////////////IMAGE FILE//////////////////////////
    //////////////////////////////////////////////////////
  }else if(firstLine.compare("%MaCI_Image log file\n") == 0){
    //Create module and add it to player
    MaCI::Image::CImageServer *imageServer = new MaCI::Image::CImageServer(aMaCICtrlServer,iImageMinor);
    iImageMinor++;
    ImageFilePlayer *imagePlayer = new ImageFilePlayer(imageServer,aFilename);
    pModule =static_cast<PlayerModule *>(imagePlayer);

    dPrint(3,"Image file found!Created Image file player");
    
    
  }else{
    dPrint(1,"Didn't recognize file '%s'.. ignoring.",aFilename.c_str());

  }
  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////
  

  return pModule;
};
